package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PdfAdapter;
import flc.ast.bean.PdfBean;
import flc.ast.databinding.ActivityPdfConvertPicBinding;
import flc.ast.dialog.ConvertTipDialog;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.MediaInfo;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.utils.RxUtil;

public class PdfConvertPicActivity extends BaseAc<ActivityPdfConvertPicBinding> {

    private PdfAdapter pdfAdapter;
    private String selPath = "";
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_pdf_convert_pic;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPdfConvertPicBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvImport.setOnClickListener(this);

        mDataBinding.rvPdfList.setLayoutManager(new LinearLayoutManager(mContext));
        pdfAdapter = new PdfAdapter();
        mDataBinding.rvPdfList.setAdapter(pdfAdapter);
        pdfAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvImport://导入
                if (TextUtils.isEmpty(selPath)) {
                    ToastUtils.showShort(getString(R.string.please_sel_pdf));
                    return;
                }
                showConvertTipDialog();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        pdfAdapter.getItem(oldPosition).setSelected(false);
        oldPosition = position;
        pdfAdapter.getItem(position).setSelected(true);
        pdfAdapter.notifyDataSetChanged();
        selPath = pdfAdapter.getItem(position).getPath();
        mDataBinding.tvImport.setText(getString(R.string.import_num, 1));
    }


    @Override
    protected void initData() {
        mDataBinding.tvImport.setText(getString(R.string.import_num, 0));
        getPdfData();
    }

    //获取文档数据
    private void getPdfData() {
        List<PdfBean> listShow = new ArrayList<>();
        //设置获取的数据类型 只获取pdf
        List<String> type = new ArrayList<>();
        type.add("application/pdf");
        RxUtil.create(new RxUtil.Callback<List<MediaInfo>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MediaInfo>> emitter) {
                List<MediaInfo> infoList = MediaLoader.loadDoc(type);
                emitter.onNext(infoList);
            }

            @Override
            public void accept(List<MediaInfo> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    for (MediaInfo bean : selectMediaEntities) {
                        listShow.add(new PdfBean(bean.getPath()));
                    }
                }
                if (listShow.size() > 0) {
                    pdfAdapter.setList(listShow);
                    mDataBinding.rvPdfList.setVisibility(View.VISIBLE);
                    mDataBinding.ivPdfNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPdfList.setVisibility(View.GONE);
                    mDataBinding.ivPdfNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void showConvertTipDialog() {
        ConvertTipDialog dialog = new ConvertTipDialog(mContext);
        dialog.type = 0;
        dialog.selPath = selPath;

        dialog.show();
    }

}